package com.scope.imageFilters;

import org.opencv.imgproc.Imgproc;

public class ThreshParam {
	public ThreshParam (double value, double maxValue, ThreshType tresh) {
		mValue = value;
		mMaxValue = maxValue;
		mThreshType = tresh;
	}
	
	private double mValue;
	public double getThreshValue() {
		return mValue;
	}
	
	private double mMaxValue;
	public double getMaxValue() {
		return mMaxValue;
	}
	
	private ThreshType mThreshType;
	public ThreshType getTreshType() {
		return mThreshType;
	}
	
    @Override
    public String toString() {
        return Double.toString(mValue) + QUERY
        		+ Double.toString(mMaxValue) + QUERY
        		+ mThreshType.toString();
    }

    public static ThreshParam fromString(String str) {
        String[] parts = str.split(MorphoParam.QUERY);
        if (parts.length == 3) {
        	return new ThreshParam(
        			Double.parseDouble(parts[0]),
        			Double.parseDouble(parts[1]),
        			ThreshType.fromString(parts[2])
        	);
        } else {
            throw new IllegalArgumentException(str);
        }
    }

    public final static String QUERY = ";;";
	public final static int DFLT_MAX = 255;
	
	public enum ThreshType {
	    BINARY(Imgproc.THRESH_BINARY),
	    BINARY_INV(Imgproc.THRESH_BINARY_INV),
	    TOZERO(Imgproc.THRESH_TOZERO),
	    TOZERO_INV(Imgproc.THRESH_TOZERO_INV),
	    TRUNC(Imgproc.THRESH_TRUNC);

	    private final int mValue;
	    int getValue() {
	        return mValue;
	    }

	    ThreshType(int value) {
	        mValue = value;
	    }

	    @Override
	    public String toString() {
	        return Integer.toString(mValue);
	    }

	    public static ThreshType fromInteger(int val) {
	        switch (val) {
	            case Imgproc.THRESH_BINARY:
	                return BINARY;
	            case Imgproc.THRESH_BINARY_INV:
	                return BINARY_INV;
	            case Imgproc.THRESH_TOZERO:
	                return TOZERO;
	            case Imgproc.THRESH_TOZERO_INV:
	            	return TOZERO_INV;
	            case Imgproc.THRESH_TRUNC:
	            	return TRUNC;
	            default:
	                throw new IllegalArgumentException();
	        }
	    }

	    public static ThreshType fromString(String str) {
	        try {
	            return ThreshType.fromInteger(Integer.parseInt(str));
	        } catch (Exception e) {
	            throw new IllegalArgumentException(str);
	        }
	    }
	}
}
